var SEA = require('./root');
var shim = require('./shim');
//var S = require('./settings');
var aeskey = require('./aeskey');

SEA.encrypt =
  SEA.encrypt ||
  (async (data, pair, cb, opt) => {
    try {
      opt = opt || {};
      const key = pair.epriv || pair;
      const msg = JSON.stringify(data);
      const rand = { s: shim.random(8), iv: shim.random(16) };
      const ct = await aeskey(key, rand.s, opt).then(aes =>
        shim /*shim.ossl ||*/.subtle
          .encrypt(
            {
              // Keeping the AES key scope as private as possible...
              name: opt.name || 'AES-GCM',
              iv: new Uint8Array(rand.iv)
            },
            aes,
            new shim.TextEncoder().encode(msg)
          )
      );
      const r =
        'SEA' +
        JSON.stringify({
          ct: shim.Buffer.from(ct, 'binary').toString('utf8'),
          iv: rand.iv.toString('utf8'),
          s: rand.s.toString('utf8')
        });

      if (cb) {
        try {
          cb(r);
        } catch (e) {
          // FIXME: Throw when not the expected error
          //if (e !== TODO) throw e;
          console.log(
            'If this error occured during normal operation validate it here',
            e
          );
        }
      }
      return r;
    } catch (e) {
      SEA.err = e;
      if (cb) {
        cb();
      }
      return;
    }
  });

module.exports = SEA.encrypt;
